#include <54func.h>
//目标：实现两个买票窗口
typedef struct shareRes_s{
    int sticketNum;//票
    pthread_mutex_t mutex;//互斥锁
    pthread_cond_t cond;//条件变量
}shareRes_t;

void *sellWindow1(void *arg)
{
    shareRes_t *pshareRes = (shareRes_t *)arg;
    while(pshareRes->sticketNum>0)
    {
        printf("Before window1：ticketNum = %d\n",pshareRes->sticketNum);
        pthread_mutex_lock(&pshareRes->mutex);
        --pshareRes->sticketNum;
        pthread_mutex_unlock(&pshareRes->mutex);
        printf("After window1：ticketNum = %d\n",pshareRes->sticketNum);
        //sleep(1);
    }
    pthread_exit(NULL);
}

void *sellWindow2(void *arg)
{
    shareRes_t *pshareRes = (shareRes_t *)arg;
    while(pshareRes->sticketNum>0)
    {
        printf("Before window2：ticketNum = %d\n",pshareRes->sticketNum);
        pthread_mutex_lock(&pshareRes->mutex);
        --pshareRes->sticketNum;
        pthread_mutex_unlock(&pshareRes->mutex);
        printf("After window2：ticketNum = %d\n",pshareRes->sticketNum);
        //sleep(1);
    }
    pthread_exit(NULL);
}

int main()
{
    shareRes_t shareRes;
    shareRes.sticketNum = 20;
    pthread_mutex_init(&shareRes.mutex,NULL);//初始化锁
    pthread_t tid1;
    pthread_t tid2;
    pthread_create(&tid1,NULL,sellWindow1,&shareRes);
    pthread_create(&tid2,NULL,sellWindow2,&shareRes);

    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    return 0;
}
